package com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.proof.subview

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import android.widget.TextView
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import org.json.JSONArray
import org.json.JSONObject

interface IProofGhanaContactListener {
    fun onClick(tid: String)
}

class ProofGhanaContact : LinearLayout {

    private lateinit var dataList: MutableList<JSONObject>

    private lateinit var title: TextView
    private lateinit var relation: ProofGhanaSelect
    private lateinit var name: ProofGhanaInput
    private lateinit var phone: ProofGhanaInput

    private var bindData: JSONObject? = null
    private var onItemClick: IProofGhanaContactListener? = null

    constructor(context: Context) : super(context) {
        initializeView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initializeView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initializeView(context)
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initializeView(context: Context) {
        inflate(context, R.layout.view_ghana_proof_contact, this)
        orientation = VERTICAL
        dataList = mutableListOf()

        title = findViewById(R.id.title)
        relation = findViewById(R.id.proofContactRelation)
        name = findViewById(R.id.proofContactName)
        phone = findViewById(R.id.proofContactPhone)

    }

    fun setBindData(jsonObject: JSONObject, onClick: IProofGhanaContactListener?) {
        this.onItemClick = onClick
        this.bindData = jsonObject
        this.title.text = jsonObject.optString("title")

        val dataList = jsonObject.optJSONArray("dataList") ?: JSONArray()
        if (dataList.length() > 0) {
            this.relation.setBindData(
                (dataList[0] as JSONObject),
                object : IProofGhanaSelectListener {
                    override fun onClick(tid: String) {
                        onItemClick?.onClick(tid)
                    }
                })
        }
        if (dataList.length() > 1) {
            this.name.setBindData((dataList[1] as JSONObject))
        }
        if (dataList.length() > 2) {
            this.phone.setBindData((dataList[2] as JSONObject))
        }
    }
}
